<?php
class right_manage {

	var $CI;
	public function __construct() {
		$this->CI =& get_instance();
	}


	/**
	 *
	 *
	 * @name check_right
	 * @date 2013-3-21
	 * @author eifel
	 * @param string 权限值
	 * @return boolean 是否含有权限
	 * @version 1.0
	 * @description 根据权限值查询用户是否含有权限
	 */
	public function check_right($action,$user_id="") {
		$this->CI->load->library("common/role_manage");
		if ($user_id=="") {
			$user_id=$_SESSION['user_id'];
		}
		if ($user_id==1) {
			return true;
		}else{
			$this->CI->load->model("common/adm_right_model");
			return $this->CI->adm_right_model->check_right($action,$user_id);
		}
	}
	/**
	 *
	 *
	 * @name get_module_right
	 * @date 2013-3-21
	 * @author eifel
	 * @param
	 * @return array权限模块列表
	 * @version 1.0
	 * @description 返回权限模块列表
	 */
	public function get_module_right() {
		$this->CI->load->model("common/adm_right_model");
		return $this->CI->adm_right_model->get_module_right();
	}
	/**
	 *
	 *
	 * @name check_name
	 * @date 2013-3-21
	 * @author eifel
	 * @param string 权限名称,string 权限编号
	 * @return array 检验权限的操作结果
	 * @version 1.0
	 * @description 检验权限名称
	 */

	public function check_name($name,$right_id) {
		if ($name=="") {
			$result['result']	=false;
			$result['message']	="功能名称不能为空";
			return $result;
		}
		if (mb_strlen($name,"utf-8")>20) {
			$result['result']	=false;
			$result['message']	="功能名称不能超过20个字";
			return $result;
		}
		$this->CI->load->model("common/adm_right_model");
		$right_info	=$this->CI->adm_right_model->query_right_by_name($name);
		if ($right_info==null) {
			$result['result']	=true;
			$result['message']	="这个功能名称可以使用";
			return $result;
		}
		if ($right_info['RIGHT_ID']!=$right_id) {
			$result['result']	=false;
			$result['message']	="功能名称已经存在";
			return $result;
		}else{
			$result['result']	=true;
			$result['message']	="这个功能名称可以使用";
			return $result;
		}
	}

	public function check_action($action,$right_id) {
		if ($action=="") {
			$result['result']	=false;
			$result['message']	="功能代码不能为空";
			return $result;
		}
		if (mb_strlen($action,"utf-8")>20) {
			$result['result']	=false;
			$result['message']	="功能代码不能超过20个字";
			return $result;
		}
		$this->CI->load->model("common/adm_right_model");
		$right_info	=$this->CI->adm_right_model->query_right_by_action($action);
		if ($right_info==null) {
			$result['result']	=true;
			$result['message']	="这个功能代码可以使用";
			return $result;
		}
		if ($right_info['RIGHT_ID']!=$right_id) {
			$result['result']	=false;
			$result['message']	="功能代码已经存在";
			return $result;
		}else{
			$result['result']	=true;
			$result['message']	="这个功能代码可以使用";
			return $result;
		}
	}
	/**
	 *
	 *
	 * @name check_url
	 * @date 2013-3-21
	 * @author eifel
	 * @param string 权限地址,string 权限编号
	 * @return array 检验权限地址的操作结果
	 * @version 1.0
	 * @description 检验权限地址
	 */
	public function check_url($url,$right_id) {
		if ($url=="") {
			$result['result']	=false;
			$result['message']	="功能地址不能为空";
			return $result;
		}
		if (mb_strlen($url,"utf-8")>100) {
			$result['result']	=false;
			$result['message']	="功能地址不能超过100个字";
			return $result;
		}
		$this->CI->load->model("common/adm_right_model");
		$right_info	=$this->CI->adm_right_model->query_right_by_url($url);
		if ($right_info==null) {
			$result['result']	=true;
			$result['message']	="这个功能地址可以使用";
			return $result;
		}
		if ($right_info['RIGHT_ID']!=$right_id) {
			$result['result']	=false;
			$result['message']	="功能地址已经存在";
			return $result;
		}else{
			$result['result']	=true;
			$result['message']	="这个功能地址可以使用";
			return $result;
		}
	}

	/**
	 *
	 *
	 * @name add_right
	 * @date 2013-3-21
	 * @author eifel
	 * @param array 功能信息
	 * @return array
	 * @version 1.0
	 * @description
	 */
	public function add_right($right_data) {
		$check_name_result	=$this->check_name($right_data['name'], "");
		if ($check_name_result['result']==false) {
			return $check_name_result;
		}
		$check_url_result	=$this->check_url($right_data['url'], "");
		if ($check_url_result['result']==false) {
			return $check_url_result;
		}
		$this->CI->load->model("common/adm_right_model");
		$right_id	=$this->CI->adm_right_model->add_right($right_data);
		$result['result']	=true;
		$result['message']	=base_url()."index.php/common_right_manage/add_right_end?right_id=".$right_id;
		return $result;
	}

	/**
	 *
	 *
	 * @name query_right_info
	 * @date 2013-3-21
	 * @author eifel
	 * @param string 功能编号
	 * @return array 功能信息
	 * @version 1.0
	 * @description 返回功能信息
	 */
	public function query_right_info($right_id) {
		$this->CI->load->model("common/adm_right_model");
		return  $this->CI->adm_right_model->query_right_info($right_id);
	}


	/**
	 *
	 *
	 * @name get_url_list
	 * @date 2013-3-21
	 * @author eifel
	 * @param
	 * @return array 功能地址列表
	 * @version 1.0
	 * @description  返回功能地址列表
	 */
	public function get_url_list() {
		$this->CI->load->model("common/adm_right_model");
		return $this->CI->adm_right_model->get_url_list();
	}

	/**
	 *
	 *
	 * @name delete_right
	 * @date 2013-3-21
	 * @author eifel
	 * @param string 功能编号
	 * @return
	 * @version 1.0
	 * @description 删除功能编号
	 */
	public function delete_right($right_id) {
		$this->CI->load->model("common/adm_right_model");
		$this->CI->adm_right_model->delete_right($right_id);
	}

	/**
	 *
	 *
	 * @name modify_right
	 * @date 2013-3-21
	 * @author eifel
	 * @param  array 功能信息 string 功能编号
	 * @return array 编辑功能的操作结果
	 * @version 1.0
	 * @description 编辑功能
	 */
	public function modify_right($right_data,$right_id) {
		$check_name_result	=$this->check_name($right_data['name'], $right_id);
		if ($check_name_result['result']==false) {
			return $check_name_result;
		}
		$check_url_result	=$this->check_url($right_data['url'], $right_id);
		if ($check_url_result['result']==false) {
			return $check_url_result;
		}
		$this->CI->load->model("common/adm_right_model");
		$this->CI->adm_right_model->modify_right($right_data,$right_id);
		$result['result']	=true;
		$result['message']	=base_url()."index.php/common_right_manage/modify_right_end?right_id=".$right_id;
		return $result;
	}

	/**
	 *
	 *
	 * @name assign_action
	 * @date 2013-3-25
	 * @author eifel
	 * @param array 功能代码
	 * @return array 功能权限添加的操作结果
	 * @version 1.0
	 * @description 校验并添加功能权限,并返回操作结果
	 */
	public function assign_action($action_data) {
		$check_name_result	=$this->check_name($action_data['name'], "");
		if ($check_name_result['result']==false) {
			return $check_name_result;
		}
		$check_action_result	=$this->check_action($action_data['action'], "");
		if ($check_action_result['result']==false) {
			return $check_action_result;
		}
		if ($action_data['parent_id']=="") {
			$result['result']	=false;
			$result['message']	="你必须先选择一个功能";
			return $result;
		}
		$this->CI->load->model("common/adm_right_model");
		$right_id	=$this->CI->adm_right_model->assign_action($action_data);
		$result['result']	=true;
		$result['message']	=base_url()."index.php/common_right_manage/assign_action_end?right_id=".$right_id;
		return $result;
	}

	public function query_action_list($right_id) {
		$this->CI->load->model("common/adm_right_model");
		return $this->CI->adm_right_model->query_action_list($right_id);
	}
	/**
	 *
	 *
	 * @name modify_action
	 * @date 2013-3-25
	 * @author eifel
	 * @param array 功能权限信息,string 功能权限编号
	 * @return array 操作结果
	 * @version 1.0
	 * @description 编辑功能权限
	 */

	public function modify_action($action_data,$right_id) {
		if ($right_id=="") {
			$result['result']	=false;
			$result['message']	="你必须先选择一个功能";
			return $result;
		}
		$check_name_result	=$this->check_name($action_data['name'], $right_id);
		if ($check_name_result['result']==false) {
			return $check_name_result;
		}
		$check_action_result	=$this->check_action($action_data['action'], $right_id);
		if ($check_action_result['result']==false) {
			return $check_action_result;
		}
		$this->CI->load->model("common/adm_right_model");
		$this->CI->adm_right_model->modify_action($action_data,$right_id);
		$result['result']	=true;
		$result['message']	=base_url()."index.php/common_right_manage/modify_action_end?right_id=".$right_id;
		return $result;
	}

	/**
	 *
	 *
	 * @name query_right_list
	 * @date 2013-3-25
	 * @author eifel
	 * @param
	 * @return array 权限列表
	 * @version 1.0
	 * @description 返回权限列表
	 */
	public function query_right_list() {
		$this->CI->load->library("common/role_manage");
		$role_info =$this->CI->role_manage->query_user_role();
		$this->CI->load->model("common/adm_right_model");
		return $this->CI->adm_right_model->query_right_list();
	}


}